﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;

namespace WindowsFormsApplication1
{
    class NetStuff
    {
        public static bool DownloadFile(string httpPath, string Destination, bool TU)
        {
            try
            {
                WebRequest wr = WebRequest.Create(httpPath);
                WebResponse ws = wr.GetResponse();
                Stream str = ws.GetResponseStream();
                wr.Timeout = 650;
               
                byte[] inBuf = new byte[1000000000];
                long bytesToRead = inBuf.Length;
                long bytesRead = 0;
                if (TU == true)
                {
                    if (File.Exists(Destination) == false)
                    {

                        while (bytesToRead > 0)
                        {
                            int n = str.Read(inBuf, Convert.ToInt32(bytesRead), Convert.ToInt32(bytesToRead));
                            if (n == 0)
                            {
                                break;
                            }
                            bytesRead += n;
                            bytesToRead -= n;
                        }

                        if (File.Exists(Destination))
                        {
                            File.Delete(Destination);
                        }

                        FileStream fstr = new FileStream(Destination, FileMode.Create, FileAccess.Write);
                        fstr.Write(inBuf, 0, Convert.ToInt32(bytesRead));

                        fstr.Close();
                    }
                    str.Close();
                }
                else
                {
                    if (File.Exists(Destination) == false)
                    {

                        while (bytesToRead > 0)
                        {
                            int n = str.Read(inBuf, Convert.ToInt32(bytesRead), Convert.ToInt32(bytesToRead));
                            if (n == 0)
                            {
                                break;
                            }
                            bytesRead += n;
                            bytesToRead -= n;
                        }

                        if (File.Exists(Destination))
                        {
                            File.Delete(Destination);
                        }

                        FileStream fstr = new FileStream(Destination, FileMode.Create, FileAccess.Write);
                        fstr.Write(inBuf, 0, Convert.ToInt32(bytesRead));

                        fstr.Close();
                    }
                    str.Close();
                }
                return true;

            }

            catch (Exception ex)
            {
                Form1.ErrorLogWrite(ex.Message, Form1.SelectedGameName);
                
                return false;
            }
        }

    }
}
